package top.huhuiyu.springboot2.study.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.huhuiyu.springboot2.study.base.BaseDataResult;
import top.huhuiyu.springboot2.study.base.BaseResult;
import top.huhuiyu.springboot2.study.base.PageBean;
import top.huhuiyu.springboot2.study.base.PageResult;
import top.huhuiyu.springboot2.study.entity.FootballClub;
import top.huhuiyu.springboot2.study.mapper.FootballClubMapper;
import top.huhuiyu.springboot2.study.service.FootballClubService;

import java.util.List;

/**
 * @author 胡辉煜
 */
@Service
@RequiredArgsConstructor
public class FootballClubServiceImpl implements FootballClubService {

  private final FootballClubMapper footballClubMapper;

  @Override
  public BaseResult update(FootballClub club) {
    int i = footballClubMapper.update(club);
    return i == 1 ? BaseResult.ok("修改成功") : BaseResult.fail("修改失败");
  }

  @Override
  public BaseResult delete(Integer fcid) {
    int i = footballClubMapper.delete(fcid);
    return i == 1 ? BaseResult.ok("删除成功") : BaseResult.fail("删除失败");
  }

  @Override
  public BaseResult add(FootballClub club) {
    int i = footballClubMapper.add(club);
    return i == 1 ? BaseResult.ok("添加成功") : BaseResult.fail("添加失败");
  }

  @Override
  public BaseDataResult<FootballClub> queryByKey(Integer fcid) {
    FootballClub club = footballClubMapper.queryByKey(fcid);
    if (club == null) {
      return BaseDataResult.fail("没有相关数据", FootballClub.class);
    }
    return BaseDataResult.ok(club);
  }

  @Override
  public BaseDataResult<PageResult<List<FootballClub>>> query(Integer order, FootballClub club, PageBean pageBean) {
    PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
    Page<FootballClub> page = (Page<FootballClub>) footballClubMapper.query(order, club);
    pageBean.setTotal(page.getTotal());
    return BaseDataResult.ok(PageResult.instance(pageBean, page.getResult()));
  }

}
